<?php

namespace app\beautiful\controller\miniapp\staff\appointment;

use app\BaseController;

use app\beautiful\model\appointment\PerformanceModel;



class Achievement extends BaseController{

    public function getAchievementList()

    {

        $year=$this->request->param('year',date('Y',$this->request->time()));

        $month=$this->request->param('month',date('m',$this->request->time()));

        $where = [

            ['shop_id','=',$this->request->shopId],

            ['staff_id','=',$this->request->staffId],

            ['status','=',8]

        ];



        $datas=PerformanceModel::with(['items'])

            ->where($where)

            ->whereMonth('add_time',$year.'-'.$month)

            ->order(['add_time'=>'desc'])

            ->field('performance_id,commission,add_time')

            ->limit($this->request->limit)

            ->page($this->request->page)

            ->select()

            ->append(['add_time_day_format'])

            ->toArray();

        $return['datas']=$datas;

        if($this->request->page==1){

            $performance=PerformanceModel::where($where)

                ->whereMonth('add_time',$year.'-'.$month)

                ->field('count(*) as count, sum(commission) as commission')

                ->find();

            $return['total']=$performance->count ?? 0;

            $return['total_commission']=$performance->commission===null ? 0 : $performance->commission;

        }



        return $this->success($return);

    }

}

